package com.ysq.controller;

import com.ysq.controller.result.Result;
import com.ysq.entity.User;
import com.ysq.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * ClassName: UserController
 * Package: com.ysq.controller
 * Description:
 *
 * @Author YSQ
 * @Create 2023/1/5 - 14:35
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService service;

    /**
     * 获取验证码
     *
     * @param user    前端传过来的用户数据, 里面封装了phone属性, 对应邮箱账号
     * @param session 将邮件发送的验证码存起来, 方便在登录时拿出来对比
     * @return 返回统一结果封装类
     */
    @PostMapping("/sendMsg")
    public Result<String> sendMsg(@RequestBody User user, HttpSession session) throws MessagingException {
        boolean flag = service.sendMsg(user, session);
        return flag ? Result.success("验证码发送成功~~~") : Result.error("验证码发送失败~~~");
    }

    /**
     * 用户登录
     *
     * @param map     里面封装了前端发来的邮箱和验证码
     * @param session 用来获取之前发送的验证码和用户输入的做对比
     * @return 返回统一结果封装类
     */
    @PostMapping("/login")
    public Result<User> login(@RequestBody Map<String, String> map, HttpSession session) {
        //看看前端发来了啥
        log.info(map.toString());
        return service.login(map, session);
    }
}
